﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Agile.Service;
using Agile.Service.Rbac;

using Microsoft.AspNetCore.Mvc;

namespace Web.Controllers
{
    public class ClientsDataController : Controller
    {
        private readonly IMenuService _menuService;
        private readonly IMenuElementService _menuElementService;
        private readonly IRoleService _roleService;
        private readonly IOrganizeService _organizeService;
        private readonly ILoginInfo _loginInfo;
        private readonly IItemsDetailService _itemsDetailService;
        private readonly IItemsService _itemsService;
        private readonly IAuth _auth;
        public ClientsDataController(IAuth auth, IMenuService menuService,
            IMenuElementService menuElementService,
            IRoleService roleService,
            IOrganizeService organizeService,
            IItemsDetailService itemsDetailService,
            IItemsService itemsService,
            ILoginInfo loginInfo)
        {
            _auth = auth;
            _menuService = menuService;
            _menuElementService = menuElementService;
            _roleService = roleService;
            _organizeService = organizeService;
            _itemsDetailService = itemsDetailService;
            _itemsService = itemsService;
            _loginInfo = loginInfo;
        }
        [HttpGet]
        public ActionResult GetClientsDataJson()
        {
            var power = _auth.GetCurrentUser();
            var data = new
            {
                dataItems = this.GetDataItemList(),
                organize = this.GetOrganizeList(),
                role = this.GetRoleList(),
                duty = this.GetDutyList(),
                user = _loginInfo,
                authorizeMenu = this.GetMenuList(),
                authorizeButton = this.GetMenuButtonList(),
            };
            return Content(data.ToJson());
        }
        private object GetMenuButtonList()
        {
            var power = _auth.GetCurrentUser();
            var data = power.ModuleElements;// _menuElementService.GetButtonList("");
            var dataModuleId = data.Distinct(new ExtList<MenuElement>("ModuleId"));
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (MenuElement item in dataModuleId)
            {
                var buttonList = data.Where(t => t.ModuleId.Equals(item.ModuleId));
                if (!dictionary.ContainsKey(item.ModuleId))
                {
                    dictionary.Add(item.ModuleId, buttonList);
                }
            }
            return dictionary;
        }
        private object GetMenuList()
        {
            var power = _auth.GetCurrentUser();
            var list = power.Modules;// _menuService.GetMenuList("");
            return ToMenuJson(list, "00000000-0000-0000-0000-000000000000");
        }
        private string ToMenuJson(List<Menu> data, string parentId)
        {
            StringBuilder sbJson = new StringBuilder();
            sbJson.Append("[");
            var entitys = data.FindAll(t => t.ParentID == parentId).OrderBy(p => p.OrderBy);
            if (entitys.Count() > 0)
            {
                foreach (var item in entitys)
                {
                    if (string.IsNullOrWhiteSpace(item.Icon)) item.Icon = "fa fa-bars";
                    string strJson = item.ToJson();
                    strJson = strJson.Insert(strJson.Length - 1, ",\"ChildNodes\":" + ToMenuJson(data, item.ID) + "");
                    sbJson.Append(strJson + ",");
                }
                sbJson = sbJson.Remove(sbJson.Length - 1, 1);
            }
            sbJson.Append("]");
            return sbJson.ToString();
        }
        private object GetDutyList()
        {
            var data = _roleService.GetByAll(" Category=2 ");
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (Role item in data)
            {
                var fieldItem = new
                {
                    encode = item.ID,
                    fullname = item.Name
                };
                if (!dictionary.ContainsKey(item.ID))
                {
                    dictionary.Add(item.ID, fieldItem);
                }
            }
            return dictionary;
        }
        private object GetRoleList()
        {
            var power = _auth.GetCurrentUser();
            var data = power.Roles;// _roleService.GetByAll("");
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (Role item in data)
            {
                var fieldItem = new
                {
                    encode = item.ID,
                    fullname = item.Name
                };
                if (!dictionary.ContainsKey(item.ID))
                {
                    dictionary.Add(item.ID, fieldItem);
                }
            }
            return dictionary;
        }
        private object GetOrganizeList()
        {
            var power = _auth.GetCurrentUser();
            var data = power.Organize;// _organizeService.GetByAll();
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            foreach (Organize item in data)
            {
                var fieldItem = new
                {
                    encode = item.ID,
                    fullname = item.Name
                };
                if (!dictionary.ContainsKey(item.ID))
                {
                    dictionary.Add(item.ID, fieldItem);
                }
            }
            return dictionary;
        }
        private object GetDataItemList()
        {
            var itemdata = _itemsDetailService.GetByAll();
            var items = _itemsService.GetByAll();
            Dictionary<string, object> dictionaryItem = new Dictionary<string, object>();
            foreach (var item in items)
            {
                var dataItemList = itemdata.FindAll(t => t.ItemId.Equals(item.ID));
                Dictionary<string, string> dictionaryItemList = new Dictionary<string, string>();
                foreach (var itemList in dataItemList)
                {
                    dictionaryItemList.TryAdd(itemList.ID, itemList.Name);
                }
                dictionaryItem.TryAdd(item.EnCode, dictionaryItemList);
            }
            return dictionaryItem;
        }
    }
}